package com.dyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyz.domain.SysMenu;
import com.dyz.domain.SysPrivilege;
import com.dyz.mapper.SysPrivilegeMapper;
import com.dyz.mapper.SysRolePrivilegeMapper;
import com.dyz.service.ISysPrivilegeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 权限配置 服务实现类
 * </p>
 *
 * @author dyz
 * @since 2025-04-19
 */
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements ISysPrivilegeService {
    @Resource
    private SysRolePrivilegeMapper sysRolePrivilegeMapper;

    @Override
    public List<SysPrivilege> getRolePrivileges(Long menuId, Long roleId) {
        List<SysPrivilege> sysPrivileges = list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId, menuId));
        if(CollectionUtils.isEmpty(sysPrivileges)){
            return Collections.emptyList();
        }
        //当前角色拥有的权限
        for (SysPrivilege sysPrivilege : sysPrivileges) {
          List<Long> privilegeRoleIds = sysRolePrivilegeMapper.getPrivilegeRoleId(roleId);
          if(privilegeRoleIds.contains(sysPrivilege.getId())){
              sysPrivilege.setOwn(1);
          }
        }
        return sysPrivileges;
    }
}
